package com.ruoyi.bizsys.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Arrays;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.ruoyi.bizsys.domain.AwardActivity;
import com.ruoyi.bizsys.service.IAwardActivityService;
import com.ruoyi.common.enums.RspEnmu;
import com.ruoyi.common.exception.BusinessException;
import com.ruoyi.common.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.bizsys.mapper.AwardDistributionRatioMapper;
import com.ruoyi.bizsys.domain.AwardDistributionRatio;
import com.ruoyi.bizsys.service.IAwardDistributionRatioService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.util.CollectionUtils;


/**
 * 奖励分销比Service业务层处理
 *
 * @author african-loan
 * @date 2022-06-24
 */
@Service
public class AwardDistributionRatioServiceImpl extends ServiceImpl<AwardDistributionRatioMapper, AwardDistributionRatio> implements IAwardDistributionRatioService {

    @Autowired
    private IAwardActivityService awardActivityService;

    /**
     * 查询奖励分销比
     *
     * @param id 奖励分销比ID
     * @return 奖励分销比
     */
    @Override
    public AwardDistributionRatio selectAwardDistributionRatioById(Long id) {
        return this.getById(id);
    }

    /**
     * 查询奖励分销比列表
     *
     * @param awardDistributionRatio 奖励分销比
     * @return 奖励分销比
     */
    @Override
    public List<AwardDistributionRatio> selectAwardDistributionRatioList(AwardDistributionRatio awardDistributionRatio) {
        return this.list(new QueryWrapper<>(awardDistributionRatio).last("order by id desc"));
    }

    /**
     * 新增奖励分销比
     *
     * @param awardDistributionRatio 奖励分销比
     * @return 结果
     */
    @Override
    public int insertAwardDistributionRatio(AwardDistributionRatio awardDistributionRatio) {
        awardDistributionRatio.setCreateTime(DateUtils.getNowDate());
        return baseMapper.insert(awardDistributionRatio);
    }

    /**
     * 修改奖励分销比
     *
     * @param awardDistributionRatio 奖励分销比
     * @return 结果
     */
    @Override
    public int updateAwardDistributionRatio(AwardDistributionRatio awardDistributionRatio) {
        awardDistributionRatio.setUpdateTime(DateUtils.getNowDate());
        return baseMapper.updateById(awardDistributionRatio);
    }

    /**
     * 批量删除奖励分销比
     *
     * @param ids 需要删除的奖励分销比ID
     * @return 结果
     */
    @Override
    public int deleteAwardDistributionRatioByIds(Long[] ids) {
        assert ids!=null && ids.length<=0 : "ids is null";
        List<AwardActivity> activityList = awardActivityService.list(new LambdaQueryWrapper<AwardActivity>().in(AwardActivity::getDistributionRatioId,ids));
        if(!CollectionUtils.isEmpty(activityList)){
            String idsStr = activityList.stream().map(obj->String.valueOf(obj.getDistributionRatioId())).distinct().collect(Collectors.joining(","));
            Map<String,String> paramMap = new HashMap<>();
            paramMap.put("ids",idsStr);
            throw new BusinessException(RspEnmu.AWARD_DISTRIBUTION_RATIO_REL_ACTIVITY,paramMap);
        }
        return baseMapper.deleteBatchIds(Arrays.asList(ids));
    }

    /**
     * 删除奖励分销比信息
     *
     * @param id 奖励分销比ID
     * @return 结果
     */
    @Override
    public int deleteAwardDistributionRatioById(Long id) {
        return baseMapper.deleteById(id);
    }

}
